package work20241128;

import java.util.Arrays;

/**
 * @author zdl
 * @date 2024/11/28
 */
public class Demo3 {
    public static void main(String[] args) {
        //选择排序
        /**
         *  i=0 0<3 true
         *      minIndex = 0    j=1 j<4 true if(arr[1] < arr[0]) 3<4 true minIndex=1 j++
         *                      j=2 2<4 true if(arr[2] < arr[1]) 2<3 true minIndex=2 j++
         *                      j=3 3<4 true if(arr[3]<arr[2]) 1<2 true minIndex=3 j++
         *                      j=4 4<4 false
         *                      int temp  = arr[0] = 4;
         *                      arr[0] = arr[3] = 1;  [1,3,2,1]
         *                      arr[3] = 4;   i++
         *  i=1 1<3 true        [1,3,2,4]
         *      minIndex=1  j=2 2<4 true if(arr[2]<arr[1]) 2<3 true minIndex = 2 j++
         *                  j=3 3<4 true if(arr[3]<arr[2]) 4<2 false
         *                  int temp  = arr[1] = 3;
         *                  arr[1] = arr[2] = 2; ----->[1,2,2,4]
         *                  arr[2] = 3;  ---->[1,2,3,4]   i++
         *   i=2 2<3 true   [1,2,3,4]
         *      minIndex = 2    j=3 3<4 true if(arr[3]<arr[2]) 4<3 false
         *                      int temp  = arr[2] =3;
         *                      arr[2] = arr[2] = 3; --->[1,2,3,4]
         *                      arr[2] = 3;     [1,2,3,4]  i++
         *   i=3 3<3 false
         *
         */
        int[] arr = {4,3,2,1};
        for (int i = 0; i < arr.length - 1; i++) {
            int minIndex = i;
            for (int j = i+1; j < arr.length; j++) {
                if (arr[j] < arr[minIndex]){
                    minIndex = j;
                }
            }
            int temp  = arr[i];
            arr[i] = arr[minIndex];
            arr[minIndex] = temp;
        }
        System.out.println(Arrays.toString(arr));
    }
}
